package models

import (
	"fmt"
	"github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/orm"
	"strconv"
)

type AiTpl struct {
	Id        int64
	Tpl_name  string
	Owner_id  int
	Tpl_desc  string
	Parent_id int64
	Root_id   int64
}

func (a *AiTpl) TableName() string {
	return TableName("tpl")
}

func TplAdd(p *AiTpl) (int64, error) {
	return orm.NewOrm().Insert(p)
}

//获取列表
func TplGetList(page, pageSize int, tableName string, filters ...interface{}) ([]*AiTpl, int64) {
	offset := (page - 1) * pageSize
	list := make([]*AiTpl, 0)
	query := orm.NewOrm().QueryTable(TableName(tableName))
	//过滤 条件。
	if len(filters) > 0 {
		l := len(filters)
		for k := 0; k < l; k += 2 {
			query = query.Filter(filters[k].(string), filters[k+1])
		}
	}
	total, _ := query.Count()
	query.OrderBy("id").Limit(pageSize, offset).All(&list)

	return list, total

}

//获取即时通讯 模板列表
func GetImTplList(tableName string, filters ...interface{}) ([]*AiTpl, error) {
	list := make([]*AiTpl, 0)
	query := orm.NewOrm().QueryTable(TableName(tableName))
	//过滤 条件。
	if len(filters) > 0 {
		fmt.Println(filters[0].(string), filters[1])
	}
	_, err := query.OrderBy("id").All(&list)

	if err != nil {
		return nil, err
	}
	return list, nil

}

//获取即时通讯 模板列表
func GetImTplList1(tableName string, id int, filters ...interface{}) ([]*AiTpl, error) {
	list := make([]*AiTpl, 0)
	query := orm.NewOrm().QueryTable(TableName(tableName)).Filter("owner_id", id)
	//过滤 条件。
	if len(filters) > 0 {
		fmt.Println(filters[0].(string), filters[1])
	}
	_, err := query.OrderBy("id").All(&list)

	if err != nil {
		return nil, err
	}
	return list, nil

}

//根据ID查询
func TplGetById(id int) (*AiTpl, error) {
	r := new(AiTpl)
	err := orm.NewOrm().QueryTable(TableName("tpl")).Filter("id", id).One(r)
	if err != nil {
		return nil, err
	}
	return r, nil
}

//获取全部父节点的tpl
func GetAllTpl(id int,parent_id int64) ([]*AiTpl, error) {
	list := make([]*AiTpl, 0)
	cond := orm.NewCondition()
	cond1 := cond.And("root_id", parent_id).Or("id", parent_id)
	cond2 := cond.AndCond(cond1).And("owner_id", id)
	query := orm.NewOrm().QueryTable(TableName("tpl")).SetCond(cond2)

	_, err := query.OrderBy("-id").All(&list)

	if err != nil {
		return nil, err
	}
	return list, nil
}

//获取全部tpl
func GetAllTpls(id int) ([]*AiTpl, error) {
	list := make([]*AiTpl, 0)
	query := orm.NewOrm().QueryTable(TableName("tpl")).Filter("owner_id", id)
	_, err := query.OrderBy("-id").All(&list)

	if err != nil {
		return nil, err
	}
	return list, nil
}

//更新数据库
func (a *AiTpl) Update(fields ...string) error {
	if _, err := orm.NewOrm().Update(a, fields...); err != nil {
		return err
	}
	return nil
}

//删除数据库
func (a *AiTpl) Delete(fields ...string) error {
	if _, err := orm.NewOrm().Delete(a, fields...); err != nil {
		return err
	}
	return nil
}

//删除tql
func DeleteProcess(tpl int) error {
	sql := "DELETE FROM ai_process WHERE owner_tpl_id=" + strconv.Itoa(tpl)
	_, err := orm.NewOrm().Raw(sql).Exec()
	if err != nil {
		ErrorLog(err)
		return err
	}
	return nil
}

func QueryProcessStartNode(tpl int) error {
	var id int64
	SqlStr := "SELECT id FROM ai_process WHERE owner_tpl_id=" + strconv.Itoa(tpl) + " and node=0"

	err := orm.NewOrm().Raw(SqlStr).QueryRow(&id)
	if err != nil {
		logs.Error("获取子节点 ai_process 156 err", err)
		return err
	}

	SqlStr = "with recursive u as " +
		"(select id from ai_process where parent_id=" + strconv.FormatInt(id, 10) + "or id=" + strconv.FormatInt(id, 10) + " union  select b.id from ai_process b,u where b.parent_id=u.id) " +
		"delete from  ai_process where id in (select id from u)"
	_, err = orm.NewOrm().Raw(SqlStr).Exec()
	if err != nil {
		ErrorLog(err)
		return err
	}

	return nil
}
